/*
 * contains 2 methods which compress and decompress a file
 * decompression is expecting a file with a .gzip extention
 * and outputs a file with a .xml extention
 * compression is expecting a file with a .xml extention and
 * and outputs a file with a .gzip extention
 */
package Project2.cs342.project1.mathNotebook.data;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.swing.JOptionPane;

/**
 *
 * @author Rick
 */
public class NotebookZip {

    public static int bufferSize = 8192;

    /*
     * outputs a compressed file
     *
     * @param name of file to be compressed
     */
    public Boolean compressFile(String fileName) {
        Boolean compressionSuccessful = true;
        GZIPOutputStream zipout;
        if (fileName.contains(".xml")) {
            try {
                FileOutputStream out = new FileOutputStream(getSubxml(fileName) + ".gzip");
                zipout = new GZIPOutputStream(out);
            } catch (IOException ex) {
                compressionSuccessful = false;
                System.out.println("Could not compress 1 " + fileName);
                return compressionSuccessful;
            }

            byte[] buffer = new byte[bufferSize];
            try {

                FileInputStream in = new FileInputStream(fileName);
                int length;
                while ((length = in.read(buffer, 0, bufferSize)) != -1) {
                    zipout.write(buffer, 0, length);
                }
                in.close();
            } catch (IOException ex) {
                compressionSuccessful = false;
                System.out.println("Could not compress 2 " + fileName);
            } finally {
                try {
                    zipout.close();
                } catch (IOException ex) {
                    compressionSuccessful = false;
                }
            }
        }else{
            compressionSuccessful = false;
            JOptionPane.showMessageDialog(null, "File does not have a .xml extention \n Compression Failed!");
        }
        return compressionSuccessful;
    }
    /*
     * decompress a given file
     *
     * @param String filename name of file to be decompressed
     */

    public Boolean decompressFile(String filename) {
        Boolean decompressionSuccessful = true;
        GZIPInputStream zipin;
        if (filename.contains(".gzip")) {
            try {

                FileInputStream in = new FileInputStream(filename);
                zipin = new GZIPInputStream(in);

            } catch (IOException ex) {
                decompressionSuccessful = false;
                System.out.println("Could not open " + filename);
                return decompressionSuccessful;
            }
            byte[] buffer = new byte[bufferSize];
            try {
                FileOutputStream out = new FileOutputStream(getSubGZip(filename) + ".xml");
                int length;
                while ((length = zipin.read(buffer, 0, bufferSize)) != -1) {
                    out.write(buffer, 0, length);
                }
                out.close();
            } catch (IOException ex) {
                decompressionSuccessful = false;
                System.out.println("Could not open " + filename);
            }
            try {
                zipin.close();
            } catch (IOException ex) {
                decompressionSuccessful = false;
            }
        } else {
            decompressionSuccessful = false;
            JOptionPane.showMessageDialog(null, "File does not have a .gzip extention \n Decompression Failed!");

        }
        return decompressionSuccessful;
    }

    /**
     * used to remove the .xml extention
     *
     * @param str name of incoming file
     * @return filename without .xml
     */
    public String getSubxml(String str) {
        int tmp = str.length() - 4;
        System.out.println(str.substring(0, tmp) + ".xml");
        return str.substring(0, tmp);
    }

    /**
     *   used to remove the .gzip extention
     * @param str name of incoming file
     * @return filename without .gzip
     */
    public String getSubGZip(String str) {

        int tmp = str.length() - 5;
        System.out.println(str.substring(0, tmp) + ".gzip");
        return str.substring(0, tmp);
    }

    /**
    public static void main(String[] args) {
        NotebookZip zip = new NotebookZip();
        zip.compressFile("test.xml");
        zip.decompressFile("test.zip");


//        String str = "123456.gzip";
//        System.out.println(str.substring(str.length() - 5, str.length()));
    }
     **/
}
